window.ROOT = "";
(function() {
	$("#userPic").error(function() { 
		$(this).attr("src", window.rootPath+"/img/nopicture.png"); 
		$(this).error=null;
    	}); 
	var serverData = {};
	var serverTable;
	serverData.userId = $('#userId').val();
	jQuery(document).ready(function() {
		var pictureAddress = jQuery('#pictureAddress').val();
		if(pictureAddress != ''){
			pictureAddress = window.rootPath+pictureAddress;
		}else{
			pictureAddress = window.rootPath+"/img/nopicture.png";
		}
		$('#userPic').attr("src", window.rootPath+"/img/nopicture.png"); 
		serverTable = jQuery('#dataTable').DataTable({
			"serverSide" : true,
			"ordering" : false,
			"ajax" : {
				"dataType" : "json",
				"type" : "get",
				"cache" : false,
				"url" : window.rootPath
						+ '/ui/visitorRewardProcessing/getProcessingPage',
				"data" : function(d) {
					return $.extend({}, d, serverData);
				}
			},
			columns : [
				{
					data : null,
					defaultContent : ""
				},
				 {
                    data: 'operationTime',
                    render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD HH:mm');
                        else
                            return '';
                    }
                },
				{
					data : 'operator.name'
				},
				{
					data : 'details'
				},
				{
					data : 'rewardValue'
				},
				{
                    data:null,
                    defaultContent: "",
                    render: function(data, type, full, meta){
                        var operate ='<button type="button" class="btn btn-sm btn-info editBtn" title="查看详情"><i class="fa fa-search"></i></button>';
                        return operate;
                    }
                }],
			"drawCallback" : function(oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length
						+ oSettings._iDisplayStart;
	
				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$(
							'td:eq(0)',
							oSettings.aoData[oSettings.aiDisplay[k]].nTr)
							.text(i + 1);
					k++;
				}
	
				serverTable.off('click', 'tr');
				serverTable.on('click','tr',function(event) {
					event = event || window.event;
					var target = event.target || event.scrElement;
					var data = serverTable.row(this).data();
					if(target.className.indexOf('fa-search')!=-1||target.className.indexOf('editBtn')!=-1){
						editInfo(data);
					}
					return;
				});
			}
		});
		
		var editInfo=function(data){
			if(!data) return;
			jQuery('#id').val(data.id);
			jQuery('#rewardValue').val(data.rewardValue);
			jQuery('#details').val(data.details);
			addDisabled();
			setButtonShow(true,true,false);
		}
	});
	
	 function addDisabled(){
		    jQuery('#rewardValue').attr('disabled',true);
		    jQuery('#details').attr('disabled',true);
	 }

    function removeDisabled(){
    		jQuery('#rewardValue').removeAttr("disabled");
    		jQuery('#details').removeAttr("disabled");
    }
    
    function setButtonShow(addBtn,editInfoBtn,saveBtn){
		if(addBtn){
			jQuery('.addBtn').removeAttr('disabled'); 
		}else{
			jQuery('.addBtn').attr('disabled',true);
		}
		if(editInfoBtn){
			jQuery('.editInfoBtn').removeAttr('disabled'); 
		}else{
			jQuery('.editInfoBtn').attr('disabled',true);
		}
		if(saveBtn){
			jQuery('.saveBtn').removeAttr('disabled'); 
		}else{
			jQuery('.saveBtn').attr('disabled',true);
		}
}
	//新增
    jQuery('.addBtn').on('click', function () {
    		jQuery('#id').val('');
		jQuery('#rewardValue').val('');
		jQuery('#details').val('');
		removeDisabled();
		setButtonShow(true,false,true);
    });
    
    //编辑
    jQuery('.editInfoBtn').on('click',function(){
		setButtonShow(true,false,true);
		removeDisabled();
    });
    
    //新增，编辑保存
    jQuery('.saveBtn').on('click',function(){
        if(jQuery("#addForm") && !jQuery("#addForm").Validform({tiptype:3, showAllError:true}).check(false)){
            return;
        };
        var id = jQuery('#id').val();
        if(id!=''){
        	$("#beforeUpd").val(parseFloat(jQuery('#rewardValue').val()));
        }
        var d = {
        		 "id":id,
        		 "rewardValue":jQuery('#rewardValue').val(),
        		 "details":jQuery('#details').val()
        };
        var url=window.rootPath+'/ui/visitorRewardProcessing/save/'+jQuery('#userId').val();
        $("#right").mLoading("show");
        var rv= parseFloat(jQuery('#rewardValue').val());
        var userRV=parseFloat($("#userLable").text());
        var dis=userRV-rv;
	       if(id!=''){
	    	   dis=dis+parseFloat($("#beforeUpd").val());
	       }
        
        if(dis<0){
        	alert("用户积分不足");
        	$("#right").mLoading("hide");
        }else{
        jQuery.ajax({
            type: 'post',
            url: encodeURI(url),
            data: d,
            dataType: 'json',
            success: function (data) {
            		message(data.msg);
            		if(data.code == 1){
            		//	$("#userLable").text(dis);
                    serverTable.draw(false);
                    jQuery('.addBtn').click();
            		}
            		$("#right").mLoading("hide");
            }
        });}
    });
})();